package com.mew.slots.core.game.zzdzz.service;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.common.exception.ResponseCode;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.zzdzz.dto.ZzdzzInRoom;
import com.mew.slots.core.game.zzdzz.dto.ZzdzzInRoomList;
import com.mew.slots.core.game.zzdzz.message.ZzdzzBonusPlayResponseMessage;
import org.springframework.stereotype.Service;

/**
 * @author dylin
 * @create 2021/1/29 0029
 */
@Service
public class ZzdzzBonusService {
    public int[] courseWes={-1,0,1,-1,0,1,2,-1,0,1,2,3,-1,0,1,2,3,4,-1};
    public ZzdzzBonusPlayResponseMessage play(StrategyGameVO player){
        ZzdzzBonusPlayResponseMessage message=new ZzdzzBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        if(winFree.getBounsNumber()<1){
            player.setStat(ResponseCode.BounsCanNotPlay.code());
            return message;
        }
        ZzdzzInRoomList voList = JSON.parseObject(winFree.getJson(), ZzdzzInRoomList.class);
        ZzdzzInRoom vo=null;
        for (ZzdzzInRoom zzdzzInRoom : voList.getZzdzzInRoomList()) {
            if(zzdzzInRoom.getBet()==player.getWinFree().getBet()){
                vo=zzdzzInRoom;
            }
        }
        vo.setProgressBar(0);
        playGame(message,vo);

        player.setBetNumber(0);
        player.setInBetNumber(0);
        player.setWinType(SlotsWinType.Special.code());
        player.setPaymentNumber(message.getWinGold());
        player.setBetTimes(0);
        player.iconResult(new int[]{
                message.getRouletteIndex()
        });
        winFree.setJson(JSON.toJSONString(voList));
        //重置json
        winFree.setBounsNumber(0);
        return message;
    }
    private void playGame(ZzdzzBonusPlayResponseMessage message, ZzdzzInRoom vo){
        int rouletteIndex= RandomUtil.randomInt(2,4);
        message.setRouletteIndex(rouletteIndex);
        message.setBankIndex(vo.getBankIndex());
        message.setWinGold(vo.getRoulettes()[rouletteIndex]);
        int we=courseWes[vo.getCourse()];
        if(we!=-1&&rouletteIndex==3){
           if(we==1){
               //平均压注的 1-2
              long weAddCoin=vo.getBet()*50* RandomUtil.randomInt(1,3);

              message.setWeAddCoin(weAddCoin);
              vo.setWeAddCoin(weAddCoin);
           }
           message.setWe(we);
           vo.getWes().add(we);
        }
    }

    public static void main(String[] args) {
        ZzdzzInRoom vo=new ZzdzzInRoom();
        vo.setRoulettes(new long[]{1000,2000,3000});
        StrategyGameVO player=new StrategyGameVO();
        WinFree winFree=new WinFree();
        winFree.setJson(JSON.toJSONString(vo));
        player.setWinFree(winFree);
        for (int i = 0; i < 10; i++) {
            winFree.setBounsNumber(1);
            System.out.println(new ZzdzzBonusService().play(player));
        }

    }
}
